home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue56 / Splat / keytext.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-01-28  |  1.4 KB  |  52 lines

  1. unit KeyText;
  2.  
  3. // Splat.
  4. // Convert a virtual key code to a string.
  5. // Copyright ⌐ 2000 Tempest Software, Inc.
  6.  
  7. interface
  8.  
  9. uses Windows, SysUtils, Menus;
  10.  
  11. function KeyCodeToText(KeyCode: Word): string;
  12. function KeyCodeToDisplay(KeyCode: Word): WideString;
  13.  
  14. implementation
  15.  
  16. // Convert a virtual key code to a file or resource name.
  17. function KeyCodeToText(KeyCode: Word): string;
  18. begin
  19.   case KeyCode of
  20.   Ord('0')..Ord('9'):
  21.     Result := 'Digit' + Chr(KeyCode);
  22.   Ord('A')..Ord('Z'):
  23.     Result := Chr(KeyCode);
  24.   Vk_NumPad0..Vk_NumPad9:
  25.     Result := 'NumPad' + Chr(Ord('0') + KeyCode - Vk_NumPad0);
  26.   else
  27.     Result := ShortCutToText(KeyCode);
  28.     if (Result = '') or (Pos(' ', Result) > 0) or not (Result[1] in ['a'..'z','A'..'Z']) then
  29.       Result := Format('Char%.2X', [KeyCode]);
  30.   end;
  31. end;
  32.  
  33. // Convert a virtual key code to a user-friendly character or key name.
  34. function KeyCodeToDisplay(KeyCode: Word): WideString;
  35. var
  36.   KeyState: TKeyboardState;
  37.   Text: array[0..3] of WideChar;
  38. begin
  39.   GetKeyboardState(KeyState);
  40.   FillChar(Text, SizeOf(Text), 0);
  41.   if ToUnicode(KeyCode, 0, KeyState, Text, SizeOf(Text) div SizeOf(WideChar), 0) > 0 then
  42.     Result := Text
  43.   else
  44.   begin
  45.     Result := KeyCodeToText(KeyCode);
  46.     if (Length(Result) = 6) and (Copy(Result, 1, 4) = 'Char') then
  47.       Result := Result + ' (' + ShortCutToText(KeyCode) + ')';
  48.   end;
  49. end;
  50.  
  51. end.
  52.